import java.util.ArrayList;
// 要转的圈数
public class Solution {

    public static ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> result = new ArrayList<>();
        ArrayList<Integer> temp;
        int row=0,col=0,row_s=0, col_s=0;
        if(matrix.length!=0){
            row = matrix.length;
            col = matrix[0].length;
        }
        int circles = (row+1)/2<(col+1)/2? (row+1)/2: (col+1)/2;
//        circles = circles<1? 1:circles;
        for(int i=0; i<circles; i++){
            temp = printMatrix_(row_s,col_s,row,col,matrix);
            row_s++;
            col_s++;
            row--;
            col--;
            for (Integer v:temp) {
                result.add(v);
            }
        }
        return result;
    }

    public static  ArrayList<Integer> printMatrix_(int row_s,int col_s,int row, int col,int[][] matrix){
        ArrayList<Integer> result = new ArrayList<>();
        for(int i=col_s; i<col; i++){
            result.add(matrix[row_s][i]);
        }
        for(int i=row_s+1; i<row; i++){
            result.add(matrix[i][col-1]);
        }
        if(row-1>row_s){
            for(int i=col-1-1;i>=col_s;i--){
                result.add(matrix[row-1][i]);
            }
        }
        if(col-1>col_s){
            for(int i=row-1-1;i>=row_s+1;i--){
                result.add(matrix[i][row_s]);
            }
        }

        return result;
    }
    public static void main(String args[]){
//        int[][] matrix={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
//        int[][] matrix={{1,2,3,4,5},{6,7,8,9,10}};
//        int[][] matrix={{1},{2},{3},{4},{5}};
        int[][] matrix={{1,2,3,4,5}};
        printMatrix(matrix);
    }
}